package com.xmy.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.study.entity.EduTask;
import com.xmy.study.service.IEduTaskService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import scala.Int;

import java.util.*;

/**
 * <p>
 * 任务表 前端控制器
 * </p>
 *
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/api/outside/eduTask")
public class EduTaskController {
    @Autowired
    @Lazy
    IEduTaskService iEduTaskService;

    /**
     * 获取课次列表，用于查询学生数据
     * @return
     */
    @ApiOperation("获取课次列表")
    @RequestMapping(value = "/getTaskNumber",method = RequestMethod.POST)
    public List<EduTask> getTaskNumber(@RequestBody Map<String, Object> reMap) {
        QueryWrapper<EduTask> eduTaskQueryWrapper = new QueryWrapper<>();
        if (reMap.get("classTypeId") != null && !reMap.get("classTypeId").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("class_type_id", reMap.get("classTypeId"));
        }
        if (reMap.get("yearClassId") != null && !reMap.get("yearClassId").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("year_class_id", reMap.get("yearClassId"));
        }
        if (reMap.get("yearPart") != null && !reMap.get("yearPart").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("year_part", reMap.get("yearPart"));
        }
        if (reMap.get("quarterNum") != null && !reMap.get("quarterNum").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("quarter_num", reMap.get("quarterNum"));
        }
        if (reMap.get("taskDate") != null && !reMap.get("taskDate").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("task_date", reMap.get("taskDate"));
        }
        eduTaskQueryWrapper.eq("deleted",0);
        eduTaskQueryWrapper.orderByAsc("task_date");
        eduTaskQueryWrapper.groupBy("task_date");
        return iEduTaskService.list(eduTaskQueryWrapper);
    }

    /**
     * 任务日期-实现：先拿出分班学生数据，for拿用半角逗号分开的字符串日期，在来查本接口
     * @return
     **/
    @ApiOperation("关联课次，拿学习数量与第几课次")
    @RequestMapping(value = "/getTaskList",method = RequestMethod.POST)
    public List<EduTask> getTaskList(@RequestBody Map<String, Object> reMap) {
        Object[] taskDate = new Object[0];
        if (reMap.get("taskDate") != null && !reMap.get("taskDate").toString().isEmpty()) {
            taskDate = reMap.get("taskDate").toString().split(",");
        }
        QueryWrapper<EduTask> eduTaskQueryWrapper = new QueryWrapper<>();
        if (taskDate.length > 0) {
            eduTaskQueryWrapper.in("task_date", taskDate);
        }
        String schoolDate = (String) reMap.get("schoolDate");
        if (schoolDate != null && !schoolDate.isEmpty()) {
            eduTaskQueryWrapper.le("task_date", isDateTime(schoolDate));
        }
        eduTaskQueryWrapper.eq("deleted",0);
        if (reMap.get("yearClassId") != null && !reMap.get("yearClassId").toString().isEmpty()) {
            eduTaskQueryWrapper.eq("year_class_id", reMap.get("yearClassId"));
        }
        eduTaskQueryWrapper.orderByAsc("task_course_num");
        return iEduTaskService.list(eduTaskQueryWrapper);
    }

    private String isDateTime(String schoolDate) {
        Calendar calendar = Calendar.getInstance();
        String[] date = schoolDate.split("-");
        calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]) - 1, Integer.parseInt(date[2]));
        int monthPart = calendar.get(calendar.MONTH);//当前月份
        int yearPart = calendar.get(calendar.YEAR);//当前年份
        int todayPart = calendar.get(calendar.DAY_OF_MONTH);//当前天数
        int week = calendar.get(calendar.DAY_OF_WEEK);
//        if (week == 1) {
//            calendar.set(yearPart, monthPart, (todayPart - 6));
//        } else {
//            calendar.set(yearPart, monthPart, (todayPart - (week - 2)));
//        }
//        int todayPartStart = calendar.get(calendar.DAY_OF_MONTH);//开始天数
//        int monthPartStart = calendar.get(calendar.MONTH) + 1;//开始月份
//        int yearPartStart = calendar.get(calendar.YEAR);//开始年份
        if (week == 1) {
            calendar.set(yearPart, monthPart, todayPart);
        } else {
            calendar.set(yearPart, monthPart, (todayPart + (7 - week) + 1));
        }
        int todayPartEnd = calendar.get(calendar.DAY_OF_MONTH);//结束天数
        int monthPartEnd = calendar.get(calendar.MONTH) + 1;//结束月份
        int yearPartEnd = calendar.get(calendar.YEAR);//结束年份
        return yearPartEnd + "-" + monthPartEnd + "-" + todayPartEnd;
    }

    @GetMapping("/getTaskForNowList")
    public Map<String,Object> getTaskForNowList(@RequestParam("yearPart") String yearPart,
                                           @RequestParam("quarterNum") String quarterNum,
                                           @RequestParam("subjectsId") String subjectsId,
                                           @RequestParam(value = "categoryId") Integer categoryId){
        QueryWrapper<EduTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part",yearPart);
        queryWrapper.eq("quarter_num",quarterNum);
        queryWrapper.eq("task_category_id",categoryId);
        queryWrapper.groupBy("task_course_num");
        queryWrapper.orderByAsc("task_course_num");
        List<EduTask> eduTaskList = iEduTaskService.list(queryWrapper);
        Long courseVersionId = 1676874588501311488L;//暂时默认
        EduTask eduTaskNow = iEduTaskService.getTaskOneMax(yearPart,quarterNum,courseVersionId,subjectsId,categoryId);
        Integer nowTaskNum = 0;
        if (eduTaskNow != null){
            nowTaskNum = eduTaskNow.getTaskCourseNum();
        }
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("taskList",eduTaskList);
        objectMap.put("nowTaskNum",nowTaskNum);

        Integer brushCount = iEduTaskService.brushCount(yearPart,quarterNum,categoryId);
        objectMap.put("brushCount",brushCount);
        return  objectMap;
    }

    @RequestMapping(value = "/getTaskNumberMax",method = RequestMethod.POST)
    public EduTask getTaskNumberMax(@RequestBody EduTask eduTask) {
        QueryWrapper<EduTask> eduTaskQueryWrapper = new QueryWrapper<>();
        if (eduTask != null) {
            if (eduTask.getClassTypeId() != null) {
                eduTaskQueryWrapper.eq("class_type_id", eduTask.getClassTypeId());
            }
            if (eduTask.getYearClassId() != null) {
                eduTaskQueryWrapper.eq("year_class_id", eduTask.getYearClassId());
            }
            if (eduTask.getYearPart() != null) {
                eduTaskQueryWrapper.eq("year_part", eduTask.getYearPart());
            }
            if (eduTask.getQuarterNum() != null) {
                eduTaskQueryWrapper.eq("quarter_num", eduTask.getQuarterNum());
            }
        }
        eduTaskQueryWrapper.eq("deleted",0);
        eduTaskQueryWrapper.orderByDesc("task_course_num");
        eduTaskQueryWrapper.last("LIMIT 1");
        return iEduTaskService.getOne(eduTaskQueryWrapper);
    }


    /**
     * 获取当煎布置任务的最大课次
     * @param yearPart
     * @param quarterNum
     * @param subjectsId
     * @param courseVersionId
     * @param categoryId
     * @return
     */
    @GetMapping("/getTaskNowMaxLessonNum")
    public Integer getTaskNowMaxNum(@RequestParam(value = "yearPart",required = false,defaultValue = "0") String yearPart,
                                    @RequestParam(value = "quarterNum",required = false,defaultValue = "0") String quarterNum,
                                    @RequestParam(value = "subjectsId",required = false,defaultValue = "0") String subjectsId,
                                    @RequestParam(value = "courseVersionId",required = false,defaultValue = "1676874588501311488") Long courseVersionId,
                                    @RequestParam(value = "categoryId",required = false,defaultValue = "1") Integer categoryId
                                    ){
        EduTask eduTask = iEduTaskService.getTaskOneNow(yearPart,quarterNum,courseVersionId,subjectsId,categoryId);
        Integer nowTaskNum = 15;//如果没有算所有的
        if (!Objects.isNull(eduTask)){
            nowTaskNum = eduTask.getTaskCourseNum();
        }
        return nowTaskNum;
    }

}
